﻿using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Zj.FrmView.Sys;

namespace Zj.FrmView
{
    public partial class frmMain : Form
    {
        private IScheduler sched;

        private string jobName = "job";
        private string groupName = "group";
        private string triggerName = "trigger";
        public frmMain()
        {
            InitializeComponent();

            ISchedulerFactory sf = new StdSchedulerFactory();
            sched = sf.GetScheduler();
            sched.Start();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            try
            {
                ClearTriggerAndTask();

                IJobDetail job = JobBuilder.Create<CrawTask>()
                    .WithIdentity(jobName, groupName)
                    .Build();
                ITrigger trigger = TriggerBuilder.Create()
                    .WithIdentity(jobName, triggerName)
                    .StartAt(DateTime.Now.AddSeconds(5))
                    .WithCronSchedule("/2 * * ? * * ")
                    .Build();

                sched.ScheduleJob(job, trigger);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
         
        }



        /// <summary>
        /// 清除定时器和计时任务
        /// </summary>
        private void ClearTriggerAndTask()
        {
            //sched.PauseJobs(groupName);
            //sched.PauseTriggers(triggerName);
            //sched.DeleteJobs(groupName);
        }

        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            sched.Shutdown();//窗体关闭，停止
        }
    }
}
